package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Training;
import com.qf.service.TrainingService;
import com.qf.utils.filter.ExceUtils;
import com.qf.utils.filter.enuml.PageEnum;
import com.qf.utils.filter.enuml.TrainingEnum;
import org.activiti.engine.impl.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DefaultValue;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * IntelliJ IDEA -> person
 * 2019/11/21 11:32
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class TrainingController {
    @Autowired
    private TrainingService trainingService;
    @RequestMapping("peixun/peixunjihua/findall")
    public String findall(@RequestParam(defaultValue = "1") int index, ModelMap map, String trainingname, String  optionselect, String trainingdateStart,String  trainingdateEnd  ){
        List<String> options = findOption();
        PageInfo<Training> pageInfo = trainingService.findall(index,PageEnum.PAGESIZE.getSize(),trainingname,optionselect,trainingdateStart,trainingdateEnd );
        map.addAttribute("pageInfo",pageInfo);
        map.addAttribute("options",options);
        map.addAttribute("trainingname",trainingname);
        map.addAttribute("optionselect",optionselect);
        map.addAttribute("trainingdateStart",trainingdateStart);
        map.addAttribute("trainingdateEnd",trainingdateEnd);
        return  "peixun/peixunjihua/list";
    }

    @RequestMapping("peixun/peixunjihua/insertTraining")
    public String   insert(Training training){
training.setCreatedate(new Date());
training.setAuditopinion("起草");
        int len = trainingService.insert(training);
        if (len > 0){
            return "redirect:/peixun/peixunjihua/findall";
        }
        return  "redirect:/peixun/peixunjihua/add.jsp";
    }



    @RequestMapping("peixun/peixunjihua/findTrainingByIdUpdate")
    public String findTrainingByIdUpdate(int id,ModelMap map){
        Training training = trainingService.selectByPrimaryKey(id);
        map.addAttribute("training",training);
        return  "/peixun/peixunjihua/update";
    }
    @RequestMapping("peixun/peixunjihua/findTrainingByIdMinxi")
    public String findTrainingByIdMinxi(int id,ModelMap map){
        Training training = trainingService.selectByPrimaryKey(id);
        map.addAttribute("training",training);
        return  "/peixun/peixunjihua/minxi";
    }

    @RequestMapping("peixun/peixunjihua/updateTraining")
    public void updateTraining (Training training, HttpServletResponse response){
        try {
            int id = training.getId();
            int len = trainingService.updateByPrimaryKeySelective(training);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (len > 0 ){
               out.print("<script>alert('修改成功!');location.href='/peixun/peixunjihua/findall';</script>");
            }
            else {
                out.print("<script>alert('修改失败!');location.href='/peixun/peixunjihua/update?id="+id+"';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/peixun/peixunjihua/deleteTrainingById")
public void deleteTrainingById(int id ,HttpServletResponse response){
        try {
            int len = trainingService.updateByIdSetState(id, TrainingEnum.DELETE.getState());
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (len > 0){
                out.print("<script>alert('删除成功!');location.href='/peixun/peixunjihua/findall';</script>");
            }
            else {
                out.print("<script>alert('删除失败!');location.href='/peixun/peixunjihua/findall';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<String>   findOption(){
        return   trainingService.findOption();
    }

}

